<?php
$PageSecurity = 4;
include_once ('includes/session.inc');

$pageSize = 20;
$action = isset ( $_POST ['action'] ) ? $_POST ['action'] : 'get';

if ($action == 'get') {
    $total = 0;
    $limit = isset ( $_POST ['limit'] ) ? $_POST ['limit'] : $pageSize;
    $start = isset ( $_POST ['start'] ) ? $_POST ['start'] : 0;

    $conds = array ();
    getConditions ( $conds, 'locked', '=', '0' );
    getConditions ( $conds, 'type_number', 'like', $_POST ['srch_type_number'] );
    getConditions ( $conds, 'type_name', 'like', $_POST ['srch_type_name'] );

    $result = _single ( 'api/type' )->getSongTypePaginates ( $limit, $start, $total, $conds );

    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;

    while ( $row = DB_fetch_array ( $result ) ) {
        $haveData = true;
        $data .= sprintf ( '{"type_id":"%s","type_number":"%s","type_name":"%s","created_date":"%s"},', $row ['type_id'], $row ['type_number'], $row ['type_name'], $row ['created_date'] );
    }

    if ($haveData) {
        $data = substr ( $data, 0, strlen ( $data ) - 1 );
    }

    $data .= '],"count":"' . json_encode ( $total ) . '"}';
    echo $data;
} elseif ($action == 'insert') {
    // Get datas:
    $data = array ();
    if (! empty ( $_POST ['type_number'] ))
        $data ['type_number'] = DB_escape_string ( trim ( $_POST ['type_number'] ) );
    if (! empty ( $_POST ['type_name'] ))
        $data ['type_name'] = DB_escape_string ( trim ( $_POST ['type_name'] ) );

    $result = array ();
    $checkNumberExisted = _single ( 'api/type' )->verifyFieldExist ( 'type_number', $data ['type_number'] );
    $result ['error'] = 0;
    if ($checkNumberExisted) {
        $result ['error'] = 1;
    } else {
        $result ['id'] = _single ( 'api/type' )->insert ( $data, $_SESSION ['UserID'], $_SESSION ['Password'] );
    }
    ;
    echo '{"success":true,"data":' . json_encode ( $result ) . '}';
} elseif ($action == 'update') {
    $data = array ();
    if (! empty ( $_POST ['field'] ))
        $data ['field'] = $_POST ['field'];
    if (! empty ( $_POST ['value'] ))
        $data ['value'] = DB_escape_string ( trim ( $_POST ['value'] ) );
    if (! empty ( $_POST ['type_id'] ))
        $data ['type_id'] = $_POST ['type_id'];

    $update_rs = _single ( 'api/type' )->update ( $data, $_SESSION ['UserID'], $_SESSION ['Password'] );
    if ($delete_rs === false) {
        echo '{"success":false ,"data":' . json_encode ( null ) . '}';
    } else {
        echo '{"success":true ,"data":' . json_encode ( $data ['type_id'] ) . '}';
    }
} elseif ($action == 'delete') {
    $data = array ();
    $delete_rs = _single ( 'api/type' )->delete ( $_POST ['type_id'], $_SESSION ['UserID'], $_SESSION ['Password'] );
    if ($delete_rs === false) {
        echo '{"success":false ,"data":' . json_encode ( null ) . '}';
    } else {
        echo '{"success":true ,"data":' . json_encode ( $_POST ['type_id'] ) . '}';
    }
}
function getConditions(&$conditions, $field, $relation, $value) {
    if (isset ( $value ) && (! empty ( $value ) || $field == 'locked')) {
        $value = ($relation == 'like') ? "N'%" . DB_escape_string ( trim ( $value ) ) . "%'" : DB_escape_string ( trim ( $value ) );
        array_push ( $conditions, array (
                'field' => $field,
                'relation' => $relation,
                'value' => $value
        ) );
    }
    ;
}
;
?>
